import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入登录页路由
import login from '../views/login'
// 导入首页路由
import home from '../views/home'
// 导入编辑资料页面路由
import edit from '../views/home/edit/index.vue'
// 导入搜索路由
import search from '../views/home/search/index.vue'
// 导入搜索结果路由
import result from '../views/home/result/index.vue'

// 导入首页嵌套路由
import index from '../views/home/index/index.vue'
import myde from '../views/home/myde/index.vue'
import questions from '../views/home/questions/index.vue'
import video from '../views/home/video/index.vue'
import { getToken, removeToken } from '../utils/token'
import { Toast } from 'vant'
import { getuserinfo } from '../api/user'
// 文章详情
import article from '../views/home/article'
// 导入用户信息接口
import store from '../store'
// 测试代码
import ceshi from '../ceshi.vue'
Vue.use(VueRouter)

const routes = [
  // // 测试
  {
    path: '/ceshi',
    name: 'ceshi',
    component: ceshi
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  // 路由重定向
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/',
    name: 'home',
    component: home,
    children: [
      {
        path: 'index',
        component: index
      },
      {
        path: 'myde',
        component: myde,
        meta: { needLoding: true }
      },
      {
        path: 'questions',
        component: questions
      },
      {
        path: 'video',
        component: video
      },
      {
        path: 'edit',
        name: 'edit',
        component: edit,
        meta: { hideshow: true, needLoding: true }
      },
      {
        path: 'article',
        name: 'article',
        component: article
      },
      {
        path: 'search',
        name: 'search',
        component: search,
        meta: { hideshow: true }
      },
      {
        path: 'result',
        name: 'result',
        component: result
      }
    ]
  }
]
// 吞掉 没有处理的 promise的错误
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) {
    // 执行之后 阻断后续代码
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
  routes
})
// 添加全局前置守卫
router.beforeEach((to, from, next) => {
  // if (to.meta.needLoding !== true) {
  //   return next()
  // }
  if (store.state.userInfo.name !== undefined) {
    return next()
  }
  const tokenboj = getToken()
  if (tokenboj === null && to.meta.needLoding !== true) {
    return next()
  }

  if (tokenboj === null && to.meta.needLoding === true) {
    Toast.fail('请先登录')
    return next({ path: '/login', query: { redirect: to.path } })
  }
  getuserinfo()
    .then(res => {
      store.commit('setUserInfo', res.data.data)
      next()
    })
    .catch(errRes => {
      // console.dir(errRes) 这个方法可以打印出对象的结构
      if (errRes.response.status === 401) {
        if (to.meta.needLoding === true) {
          removeToken()
          // 提示
          Toast.fail('请先登录')
          return next({ path: '/login', query: { redirect: to.path } })
        }
        return next()
      }
    })
})
export default router
